package com.keur.money.ui.main.fm

import android.view.LayoutInflater
import android.view.View
import com.keur.money.R
import com.keur.money.base.utils.TitleBarUtil
import com.keur.money.config.KeurConfigs
import com.keur.money.databinding.FmMullaUserBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.ProFm
import com.keur.money.ui.KeurPaPoyAct
import com.keur.money.ui.user.act.KeurUserCheckInAct
import com.keur.money.ui.user.act.KeurUserCouponAct
import com.keur.money.ui.user.act.KeurUserFeedbackAct
import com.keur.money.ui.user.act.KeurUserLuckyDrawAct
import com.keur.money.ui.user.act.KeurUserMessageAct
import com.keur.money.ui.user.act.KeurUserPointsAct
import com.keur.money.ui.user.act.KeurUserProfileAct
import com.keur.money.ui.user.act.KeurUserSettingAct
import com.keur.money.ui.user.act.KeurUserVipAct
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import com.keur.money.utils.helper.KeurCustomDataHelper

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/26 16:57
 * @Annotation：个人中心
 */
class KeurUserFm: ProFm() {
    private var _binding: FmMullaUserBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    override fun getMullaRootView(inflater: LayoutInflater): View {
        _binding = FmMullaUserBinding.inflate(inflater)
        return binding.root
    }

    override fun initMullaView() {
        _binding?.contentLayout?.setPadding(0, TitleBarUtil.getStatusBarHeight(), 0, 0)

        //用户使用条款
        val termsUseUrl = KeurCustomConfigsHelper.getMullaTermsUseLink()
        if (termsUseUrl.isEmpty()){
            _binding?.termsUseLayout?.visibility = View.INVISIBLE
        }
    }

    override fun initMullaListener() {
        //客服
        _binding?.imgKefu?.setOnClickListener {
            showMullaKefuDialog("User Center Page")
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_CUSTOMER,"User Center Page")
        }

        //消息
        _binding?.imgMessage?.setOnClickListener {
            onIntent(KeurUserMessageAct::class.java)
        }

        //个人简介
        _binding?.imgAvatar?.setOnClickListener {
            onIntent(KeurUserProfileAct::class.java)
        }

        //优惠券列表
        _binding?.couponLayout?.setOnClickListener {
            onIntent(KeurUserCouponAct::class.java)
        }

        //积分明细
        _binding?.pointsLayout?.setOnClickListener {
            onIntent(KeurUserPointsAct::class.java)
        }

        //check-in
        _binding?.checkInLayout?.setOnClickListener {
            onIntent(KeurUserCheckInAct::class.java)
        }
        //抽奖
        _binding?.luckydrawLayout?.setOnClickListener {
            onIntent(KeurUserLuckyDrawAct::class.java)
        }
        //VIP等级中心
        _binding?.vipLayout?.setOnClickListener {
            onIntent(KeurUserVipAct::class.java)
        }
        //联系我们
        _binding?.connectionUsLayout?.setOnClickListener {
            _binding?.imgKefu?.callOnClick()
        }
        //意见反馈
        _binding?.adviceLayout?.setOnClickListener {
            KeurUserFeedbackAct.start(requireContext(),"")
        }
        //设置
        _binding?.settingLayout?.setOnClickListener {
            onIntent(KeurUserSettingAct::class.java)
        }
        //用户使用协议
        _binding?.termsUseLayout?.setOnClickListener {
            val showTitle = getString(R.string.terms_of_use)
            val linkUrl = KeurCustomConfigsHelper.getMullaTermsUseLink()
            KeurPaPoyAct.start(requireContext(),showTitle, linkUrl)
        }
        //隐私政策
        _binding?.privacyPolicyLayout?.setOnClickListener {
            val showTitle = getString(R.string.privacy_policy_title)
            val linkUrl = KeurConfigs.KEUR_PRIVACY_POLICY_LINK
            KeurPaPoyAct.start(requireContext(),showTitle, linkUrl)
        }
    }

    override fun initMullaData() {
        _binding?.textPoints?.text = getMullaUserPoints().toString()
        val vipGrade = getMullaUserVip()
        _binding?.textVipGrade?.text = String.format(getString(R.string.vip_grade),vipGrade.toString())
        when(vipGrade){
            1 -> {
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip1_icon)
            }
            2 -> {
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip2_icon)
            }
            else ->{
                _binding?.imgVip?.setImageResource(R.mipmap.mulla_user_vip3_icon)
            }
        }
        //设置用户昵称和头像
        showMullaUserInfo()

        //是否有新版本
        val isUpdate = KeurCustomConfigsHelper.isKeurAppUpdate()
        if (isUpdate){
            _binding?.appUpdatePoint?.visibility = View.VISIBLE
        } else {
            _binding?.appUpdatePoint?.visibility = View.GONE
        }

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_USER_CENTER_START)
    }

    /**
     * 设置用户头像和昵称
     */
    private fun showMullaUserInfo(){
        _binding?.imgAvatar?.setImageResource(KeurCustomDataHelper.getMullaCustomUserAvatar())
        _binding?.textUserName?.text = KeurCustomDataHelper.getMullaCustomUserName()
    }

    override fun onKeurEvent(event: KeurEventData) {
        when(event.eventType){
            KeurEventData.TYPE.KEUR_POINTS_REFRESHED -> { //刷新积分完成
                _binding?.textPoints?.text = getMullaUserPoints().toString()
            }
            KeurEventData.TYPE.KEUR_COUPON_REFRESHED -> { //刷新优惠券完成
                _binding?.textCouponCount?.text = getMullaUserCouponCount().toString()
            }
            KeurEventData.TYPE.KEUR_CUSTOM_DATA_REFRESHED -> {//自定义数据刷新完成
                //设置用户昵称和头像
                showMullaUserInfo()
            }
            else -> {

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_USER_CENTER_END)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}